﻿using System;
using System.Drawing;
using Gaia.Ajax.UI.WebControls.VideoPlayers;
using Gaia.WebWidgets;

public partial class _Default : System.Web.UI.Page
{
    private readonly Video[] videos = new Video[]
                     {
                         new Video(0, "Elias", 3, "http://demeter.gaiaware.net/elias.flv"),
                         new Video(1, "Palm trees, big building and sun", 20, "http://blip.tv/file/get/KimAronson-TwentySeconds59483.flv"),
                         new Video(2,"Colorful flowers and leaves.", 124, "http://blip.tv/file/get/N8inpasadena-Flowers457.flv")
                     };

    protected override void OnInit(EventArgs e)
    {
        pnlModal.Aspects.Add(new AspectModal(Color.Black, 0.5D));

        if (!IsPostBack)
        {
            drpVideos.DataSource = videos;
            drpVideos.DataValueField = "ID";
            drpVideos.DataTextField = "Name";
            drpVideos.DataBind();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            SetCurrentVideo();
        }
    }

    private void SetCurrentVideo()
    {
        lblCurrentVideo.Text = videos[drpVideos.SelectedIndex].Name;
        flowplayer.VideoFile = videos[drpVideos.SelectedIndex].Url;
        flowplayer.ControlsOverVideo = chkControlsOverVideo.Checked;
        flowplayer.ShowFullScreenButton = chkAllowFullscreen.Checked;
        flowplayer.AutoPlay = chkAutoPlay.Checked;

        if (drpVideos.SelectedIndex == 2)
        {
            // add up a CuePoint for the last video. 
            flowplayer.AddCueuePoint(new CuePoint("ShowPanel", new TimeSpan(0,0,10)));
            flowplayer.AddCueuePoint(new CuePoint("ShowInfo", new TimeSpan(0,0, 5)));
        }

    }

    protected void PlayVideo(object sender, EventArgs e)
    {
        flowplayer.Play();
    }

    /// <summary>
    /// FlowPlay dispatches this event when the state of the Player changes
    /// </summary>
    protected void flowplayer_StateChanged(object sender, FlowPlayer.StatusChangedEventArgs e)
    {
        lblStatus.Text = e.CurrentState == FlowPlayer.PlayerState.Paused ? "Video is paused" : "";
    }

    /// <summary>
    /// Dispatched when a cue point is reached
    /// </summary>
    protected void flowplayer_CuePointReached(object sender, FlowPlayer.CuePointReachedEventArgs e)
    {
        switch (e.CuePoint.Name)
        {
            case "ShowPanel":
                pnlModal.Visible = true;
                flowplayer.Pause();
                break;

            case "ShowInfo":
                lblStatus.Text = "This is informational text only and is display at CuePoint after 5 seconds";
                new Effect(lblStatus, Effect.TypeOfEffect.Highlight);
                break;
        }
    }

    /// <summary>
    /// Dispatched when a video clip is finished
    /// </summary>
    protected void flowplayer_VideoClipFinished(object sender, EventArgs e)
    {
        // Playlist logic can be kept server side. 
        int idx = drpVideos.SelectedIndex;
        drpVideos.SelectedIndex = (idx >= 0 && idx < (videos.Length -1)) ? ++idx : 0;
        SetCurrentVideo();
        flowplayer.UpdatePlayer();
    }

    protected void ClosePanel(object sender, EventArgs e)
    {
        pnlModal.Visible = false;
        flowplayer.Play();
    }

    protected void UpdatePlayer(object sender, EventArgs e)
    {
        SetCurrentVideo();
        flowplayer.UpdatePlayer();
    }
}


// <summary>
/// A Video
/// </summary>
public class Video
{
    private string name;
    private int length;
    private string url;

    private int id;
    public int ID
    {
        get { return id; }
        set { id = value; }
    }

    /// <summary>
    /// Name of Video
    /// </summary>
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    /// <summary>
    /// Length of Video in seconds
    /// </summary>
    public int Length
    {
        get { return length; }
        set { length = value; }
    }

    /// <summary>
    /// Url of Video
    /// </summary>
    public string Url
    {
        get { return url; }
        set { url = value; }
    }

    public Video(int id, string name, int length, string url)
    {
        this.id = id;
        this.name = name;
        this.length = length;
        this.url = url;
    }
}